#ifndef LEVELMANAGER_H
#define LEVELMANAGER_H

#include "LevelBlock.h"
#include "LevelBlockFactory.h"

#include <vector>

#include "Ogre.h"


//#include "PathFinding.h"

#define DEBUG 1

#include "lib/micropather.h"
#include "lib/tinyXML/tinyxml.h"


typedef struct
{
    int size;
    float pCentCrossable;

} RandomMapGenerationParams;


class BuildingObject;

class LevelManager :  public micropather::Graph
{
public:
    LevelManager();
    virtual ~LevelManager();


    void init();
    void update(double timeSinceLastFrame);
    void clear();

    void createRandomLevel(RandomMapGenerationParams _params);


    LevelBlock* getBlockPtrAt(float x, float y);
    LevelBlock* getBlockPtrAt(Ogre::Vector2 p);
    LevelBlock* getBlockPtrAt(int i, int j);

    void setBlockAt(std::string name, int i, int j);


    bool isBlockCrossable(float x, float y);
    void setBlockCrossable(float x, float y, bool _val);

    void position2index(const float & x, const float & y , float *i, float *j);



    void initPathFinding();
    std::list<Ogre::Vector2> searchPath(Ogre::Vector2 start, Ogre::Vector2 end);

    void saveMap(std::string path);
    void loadMap(std::string path);


    //pour le pathfinding avec micropather
    virtual float LeastCostEstimate( void* stateStart, void* stateEnd );
    virtual void AdjacentCost( void* state, std::vector< micropather::StateCost > *adjacent );
    virtual void  PrintStateInfo( void* state );

    LevelBlock* nodeToBlock( void* node);
    void* blockToNode(LevelBlock* block);

    Ogre::Vector2 blockToVector(LevelBlock* block);
    LevelBlock* vectorToBlock(Ogre::Vector2 vector);

    std::vector<std::string> getBlocksAvailable(){return m_blockFactory->getBlocksAvailables();}

    void saveToXML(TiXmlElement* parentElement);
    void saveToXMLFile(Ogre::String _path);
    bool loadFromXML(TiXmlElement* parentElement);
    bool loadFromXMLFile(Ogre::String _path);


    void registerBuilding(BuildingObject* _building);


protected:
private:

    LevelBlockFactory *m_blockFactory;
    std::vector< std::vector <LevelBlock*> > m_arrayBlocks;
    bool m_isInitialized;
    micropather::MicroPather *m_pather;






};

#endif // LEVELMANAGER_H
